package com.iwop.moto.controller;

import com.iwop.moto.auth.LoginAuth;
import com.iwop.moto.comm.ErrCode;
import com.iwop.moto.model.ResultModel;
import com.iwop.moto.model.SuperRecordModel;
import com.iwop.moto.model.User;
import com.iwop.moto.service.FMagicBeanService;
import com.iwop.moto.service.MCouponService;
import com.iwop.moto.service.SignService;
import com.iwop.moto.service.UserService;
import com.iwop.moto.utils.stringUtil.ObjectId;
import com.iwop.moto.utils.stringUtil.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import java.math.BigDecimal;
import java.text.SimpleDateFormat;
import java.util.*;

/**
 * liuwb 2016-07-20 08:57
 */
@Controller
public class SignController extends  BaseController {

    @Autowired
    private SignService signService;

    @Autowired
    FMagicBeanService fMagicBeanService;

    @Autowired
    UserService userService;

 /*   @Autowired
    MCouponService mCouponService;*/
    /**
     * liuwb 2016-07-21 11:17
     * 功能:签到 - 新增
     * 参数:userId 用户id
     */
    @RequestMapping("/api/sign/add")
    @ResponseBody
    @LoginAuth(isval = true)
    public ResultModel addSign(){
        SuperRecordModel SRModel = this.getSRModel();
        try {
            SRModel.put("userId",SRModel.getString("session_id"));
            if(signService.findSingNowCount(SRModel)>0){
                return getErr("今天已经签到!");
            }
            String ctype = SRModel.getString("ctype");
            SRModel.put("addtime", new Date());
            SRModel.put("sid", UUID.randomUUID().toString());
            SuperRecordModel superRecordModel=signService.getLastSign(SRModel);
            String weekOfDate = StringUtils.getWeekOfDate(new Date());
            int magic_bean=0;
            if(superRecordModel==null||((int)superRecordModel.get("signTime"))>1){
                SRModel.put("count_date",1);
                SRModel.put("magic_bean",10);
            }else {
                int signTime = 0;
                if (superRecordModel.containsKey("count_date")){
                    signTime=(int) superRecordModel.get("count_date");
                }
                SRModel.put("count_date", signTime+1);
                magic_bean=(int)superRecordModel.get("magic_bean");
            }
            if(ctype.equals("1")) {
                if (magic_bean<30){
                    magic_bean=magic_bean+10;
                }
                if (weekOfDate.equals("1")){
                    magic_bean=10;
                }
                SRModel.put("magic_bean",magic_bean);
                SRModel.put("newMagicBean", addMagicBean(SRModel));
            }
            SRModel.put("magic_bean",magic_bean);
            SRModel.put("user_id",SRModel.getString("session_id"));
            signService.insertSign(SRModel);
            return getSucceed(SRModel.get("newMagicBean"),ErrCode.SUCCEED);
        }catch (Exception e){
            e.printStackTrace();
            return getErr(ErrCode.ERROR_SYSTEM);
        }
    }

    private int addMagicBean(SuperRecordModel SRModel){
        int magic_bean = (int)SRModel.get("magic_bean");
        String userId = SRModel.getString("userId");
        User userBySid = userService.getUserBySid(userId);
        int magicBean = userBySid.getMagicBean();
        SuperRecordModel srm=new SuperRecordModel();
        srm.put("sid", ObjectId.UUID());
        srm.put("before_bean",magicBean);
        int afterBean = magicBean + magic_bean;
        srm.put("after_bean", afterBean);
        srm.put("user_id",userId);
        srm.put("beans", magic_bean);
        srm.put("types","1");
        srm.put("add_time",new Date());
        fMagicBeanService.addMagicBean(srm);
        SRModel.put("magic_bean",afterBean);
        userService.updateMagicBean(SRModel);
        return afterBean;
    }

/*    private void judgeCoupon(SuperRecordModel SRModel) {
        SuperRecordModel srm = new SuperRecordModel();
        srm.put("user_id", getParam("userId"));
        srm.put("coupon_type", "newUser");
        List<SuperRecordModel> userCouponBy = mCouponService.getUserCouponBy(srm);
        if (userCouponBy !=null && userCouponBy.size()>0){
            SuperRecordModel superRecordModel1 = userCouponBy.get(0);
            if (userCouponBy != null && userCouponBy.size() > 0 && superRecordModel1.getString("withdrawals").equals("0")) {
                BigDecimal money = (BigDecimal) superRecordModel1.get("money");
                double dMoney = money.doubleValue();
                if (dMoney == 10 && (int) SRModel.get("count_date") > 13) {
                    superRecordModel1.put("withdrawals", "1");
                    mCouponService.updateUserCoupon(superRecordModel1);
                } else if (dMoney == 5 && (int) SRModel.get("count_date") > 6) {
                    superRecordModel1.put("withdrawals", "1");

                    mCouponService.updateUserCoupon(superRecordModel1);
                }
            }
        }
    }*/

    /**
     * liuwb 2016-07-21 11:58
     * 功能:签到 - 列表
     * 参数：userId 用户Id
     * 参数：sMonth 月份(2016-07)
     */
    @RequestMapping("api/sign/list")
    @ResponseBody
    @LoginAuth(isval = true)
    public ResultModel findSignList(){
        SuperRecordModel SRModel = this.getSRModel();
        try {
            List<SuperRecordModel> lists = signService.findSignList(SRModel);
            if(lists.size() == 0){
                return getErr(ErrCode.ERROR_NOCOUNT);
            }
            User userBySid = userService.getUserBySid(SRModel.getString("userId"));
            Map<String ,Object> param=new HashMap<String ,Object>();
            param.put("signList",lists);
            int magicBean = userBySid.getMagicBean();
            param.put("magic_bean", magicBean);
            return getSucceed(param,ErrCode.SUCCEED);
        }catch (Exception e){
            e.printStackTrace();
            return getErr(ErrCode.ERROR_SYSTEM);
        }
    }



}
